﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SelfMadeMillionaire.Core.Entities;

namespace YahooFinanceWatcher.Models
{
    public class SymbolOptionsHistoryModel
    {
        public string SortOrder { get; set; }

        [DisplayName("Days in Trade")]
        [Required(ErrorMessage = "Days in Trade is required")]
        public int DaysInTradeFilter { get; set; }
        [DisplayName("Min Discount to the market")]
        [Required(ErrorMessage = "Min Discount to the market is required")]
        public double MinDiscountFilter { get; set; }
        [DisplayName("Min Annual Return")]
        public string MinAnnualReturnFieldFilter { get; set; }
        [Required(ErrorMessage = "Min Annual Return is required")]
        public double MinAnnualReturnFilter { get; set; }
        public SelectList MinAnnualReturnFieldList 
        {
            get
            {
                List<SelectListItem> retList = new List<SelectListItem>();
                retList.Add(new SelectListItem() { Text = "Bid", Value = "Bid"});
                retList.Add(new SelectListItem() { Text = "Ask", Value = "Ask"});
                retList.Add(new SelectListItem() { Text = "(Bid+Ask)/2", Value = "(Bid+Ask)/2"});
                retList.Add(new SelectListItem() { Text = "Last", Value = "Last"});

                return new SelectList(retList, "Value", "Text");
            }
        }
        [DisplayName("Min Probability")]
        [Required(ErrorMessage = "Min Probability is required")]
        public double MinProbabilityFilter { get; set; }
        public bool IsCallsFilter { get; set; }

        public IEnumerable<SymbolOptionsHistory> Hystory { get; set; }
        public List<SymbolOptionsHistory> FilteredHystory { get; set; }

        public void ApplyFilters()
        {

            var filtered = Hystory.Where(x => x.DaysInTrade > DaysInTradeFilter);
            filtered = filtered.Where(x => x.GetDiscountWhole() > MinDiscountFilter);
            if (MinAnnualReturnFieldFilter == "Bid")
                filtered = filtered.Where(x => x.BidAnnualReturn >= MinAnnualReturnFilter);
            else if (MinAnnualReturnFieldFilter == "Ask")
                filtered = filtered.Where(x => x.AskAnnualReturn >= MinAnnualReturnFilter);
            else if (MinAnnualReturnFieldFilter == "(Bid+Ask)/2")
                filtered = filtered.Where(x => x.GetBidAsk2Whole() >= MinAnnualReturnFilter);
            else if (MinAnnualReturnFieldFilter == "Last")
                filtered = filtered.Where(x => x.LastAnnualReturn >= MinAnnualReturnFilter);
            filtered = filtered.Where(x => x.Probability >= MinProbabilityFilter);
            filtered = filtered.OrderBy(x => x.Symbol).ThenBy(x => x.AddDateTime);
            FilteredHystory = filtered.ToList();
        }

        public SymbolOptionsHistoryModel()
        {
            Hystory = new List<SymbolOptionsHistory>();
            FilteredHystory = new List<SymbolOptionsHistory>(); 

            DaysInTradeFilter = 0;
            MinDiscountFilter = 0;
            MinAnnualReturnFieldFilter = "(Bid+Ask)/2";
            MinAnnualReturnFilter = 0;
            MinProbabilityFilter = 0;
            IsCallsFilter = true;
        }
    }

}